package com.home.base.util;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebClientOptions;
import com.gargoylesoftware.htmlunit.html.*;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：chenxf
 * @date ：Created in 2019/6/17 15:52
 * @description：
 * @modified By：
 * @version: $
 */
@Slf4j
public class WebClientUtil {

    private WebClient webClient = new WebClient(BrowserVersion.CHROME);
    String viewUrl="https://lgn.yy.com/lgn/oauth/authorize.do?oauth_token=30e70a34088199de8cd252f2c2776ad11b8c957eb7dbd84bb6dbe8c584f77e4262d30c411ea98d208b076ed190605d34&denyCallbackURL=http://www.yy.com/login/udbCallback?cancel=1&cssid=5719_1&regCallbackURL=//www.yy.com/login/udbCallback&UIStyle=xelogin&rdm=0.5250099742308432";

    private static Map<String,String> cookie=new HashMap<>();


    public  WebClientUtil( ){

        WebClientOptions clientOptions = webClient.getOptions();
        // 设置webClient的相关参数
        clientOptions.setJavaScriptEnabled(true);
        clientOptions.setCssEnabled(false);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        clientOptions.setTimeout(35000);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        clientOptions.setThrowExceptionOnScriptError(false);
    }

    public void home() throws Exception{
        HtmlPage htmlPage=webClient.getPage("http://www.yy.com");
        List<HtmlElement> elements=htmlPage.getByXPath("//div[@class='w-head__loginbtn']");
        System.out.println("button:::"+htmlPage.asXml());
    }
    public  void login(String url) throws Exception{
        HtmlPage htmlPage=webClient.getPage(url);
        System.out.println("log:::"+htmlPage.asText());
        List<HtmlElement> elements=htmlPage.getByXPath("//div[@id='m_commonLogin']");
        System.out.println(elements.size());
        for(HtmlElement o:elements){
            List<HtmlInput> username=o.getByXPath("//input[@class='placeholder E_acct']");
            username.get(0).setValueAttribute("lc401");
            List<HtmlInput> pwd=o.getByXPath("//input[@class='placeholder E_passwd']");
            pwd.get(0).setValueAttribute("zhang.519");

            List<HtmlElement> subButton=o.getByXPath("//div[@class='form_item form_opra']/a[@class='m_button_large E_login']");
            System.out.println("button:::"+subButton.get(0).getTextContent());
            view(subButton.get(0));
        }

    }

    public void view(HtmlElement button) throws Exception{
        HtmlPage htmlPage=button.click();
        List<HtmlElement> subButton=htmlPage.getByXPath("//div[@class='m_tips png24 E_tips']");
//        System.out.println("button:::"+subButton.get(0).getTextContent());
        System.out.println("text:"+ htmlPage.asXml());
    }


    public void close() {
        webClient.close();
    }


    public static void main(String[] args){
        try {
            WebClientUtil webClientUtil=new WebClientUtil();
            webClientUtil   .home();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
